'TTT version 1.05
'Created by Tsur Willman 

'For this example you will need these objects in your form:
' - form.name = form1
' - 1 frame (not requiered)
' - 3 labels named: lblScoreX, lblScoreO, lblTurn
' - 3 labels Captioned: Player 1:, Player 2:, Turn
' - 2 command buttons named: cmdRePlay, cmdExit
' - 9 command buttons in an array named cmdBT(index) - index is moving 
'   between 1 to 9, NOT including 0. arranged in this order:
'   1 2 3
'   4 5 6
'   7 8 9

'The Code:
Option Explicit

'Declare variables
Dim score1 As Integer  'score for player 1
Dim score2 As Integer  'score for player 2
Dim turns As Integer   'sets the turn
Dim cleaner As Integer 'clean the buttons as needed
Dim player As Boolean  'false = player 1, true = player 2

'executed as one of the buttons is pressed
Private Sub cmdBT_Click(Index As Integer)
 'checking for the player
 If player = False Then
  cmdBT(Index).Caption = "X"
  lblTurn = "player 2"
  player = True
 ElseIf player = True Then
  cmdBT(Index).Caption = "O"
  lblTurn = "player 1"
  player = False
 End If
  
 'Calling to CheckVictory sub routine
 Call CheckVictory
 'disable the button, avoiding next press on it
 cmdBT(Index).Enabled = False
 'changing the focus NOT to be on a game button
 cmdExit.SetFocus
End Sub

'The form as it is loaded
Private Sub Form_Load()
 'player = player 1
 player = False
 'clearing the scores and the turn
 score1 = 0
 score2 = 0
 turns = 0
 'disableing the play again button
 cmdRePlay.Enabled = False
End Sub

'the Victory sub routine, checks if one of the players wins
'ic case of winning, calling to winning sub routine
Private Sub CheckVictory() 'declare routine
 'Check Victory for player 1 (X)
 If cmdBT(1).Caption = "X" And cmdBT(2).Caption = "X" And cmdBT(3).Caption = "X" Then
  player = False 'setting player for player 1
  Call Winning   ' calling to winning sub routine
 ElseIf cmdBT(4).Caption = "X" And cmdBT(5).Caption = "X" And cmdBT(6).Caption = "X" Then
  player = False
  Call Winning
 ElseIf cmdBT(7).Caption = "X" And cmdBT(8).Caption = "X" And cmdBT(9).Caption = "X" Then
  player = False
  Call Winning
 ElseIf cmdBT(1).Caption = "X" And cmdBT(4).Caption = "X" And cmdBT(7).Caption = "X" Then
  player = False
  Call Winning
 ElseIf cmdBT(2).Caption = "X" And cmdBT(5).Caption = "X" And cmdBT(8).Caption = "X" Then
  player = False
  Call Winning
 ElseIf cmdBT(3).Caption = "X" And cmdBT(6).Caption = "X" And cmdBT(9).Caption = "X" Then
  player = False
  Call Winning
 ElseIf cmdBT(1).Caption = "X" And cmdBT(5).Caption = "X" And cmdBT(9).Caption = "X" Then
  player = False
  Call Winning
 ElseIf cmdBT(3).Caption = "X" And cmdBT(5).Caption = "X" And cmdBT(7).Caption = "X" Then
  player = False
  Call Winning
 Else 'refers to draw(can only be on 9th move
      'while it is player 1's turn)
  turns = turns + 1
  If turns = 9 Then
  MsgBox "A draw!"
  cmdRePlay.Enabled = True
  turns = 0
 End If

 'Checking for Victory for player 2 (O)
 If cmdBT(1).Caption = "O" And cmdBT(2).Caption = "O" And cmdBT(3).Caption = "O" Then
  player = True 'setting player for player 2
  Call Winning  'calling to winning sub routine
 ElseIf cmdBT(4).Caption = "O" And cmdBT(5).Caption = "O" And cmdBT(6).Caption = "O" Then
  player = True
  Call Winning
 ElseIf cmdBT(7).Caption = "O" And cmdBT(8).Caption = "O" And cmdBT(9).Caption = "O" Then
  player = True
  Call Winning
 ElseIf cmdBT(1).Caption = "O" And cmdBT(4).Caption = "O" And cmdBT(7).Caption = "O" Then
  player = True
  Call Winning
 ElseIf cmdBT(2).Caption = "O" And cmdBT(5).Caption = "O" And cmdBT(8).Caption = "O" Then
  player = True
  Call Winning
 ElseIf cmdBT(3).Caption = "O" And cmdBT(6).Caption = "O" And cmdBT(9).Caption = "O" Then
  player = True
  Call Winning
 ElseIf cmdBT(1).Caption = "O" And cmdBT(5).Caption = "O" And cmdBT(9).Caption = "O" Then
  player = True
  Call Winning
 ElseIf cmdBT(3).Caption = "O" And cmdBT(5).Caption = "O" And cmdBT(7).Caption = "O" Then
  player = True
  Call Winning
 End If
 End If
End Sub
 
Private Sub cmdRePlay_click()
 For cleaner = 1 To 9
  'refresh all bottons
  cmdBT(cleaner).Enabled = True
  cmdBT(cleaner).Caption = ""
 Next
 
 're enable the play again command button
 cmdRePlay.Enabled = False
 'Setting the player for player 1
 player = False
End Sub

Private Sub Winning() 'declare routine
 'checking for the player
 If player = False Then
  MsgBox "player 1 wins!" 'notify for win
  score1 = score1 + 1 'add 1 to score
  turns = 0 'set turns to be 0 till next game
  lblScoreX = score1
 ElseIf player = True Then
  MsgBox "player 2 wins!"
  score2 = score2 + 1
  turns = 0
  lblScoreO = score2
 End If
   
 're enable the play again button
 cmdRePlay.Enabled = True
 'disableing all buttons at victory time
 For cleaner = 1 To 9
  cmdBT(cleaner).Enabled = False
 Next
End Sub

Private Sub cmdExit_click()
 End 'Ending the game
End Sub

  